﻿using full_leaf_framework.Visual;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace camera_and_drawable;

public class Game1 : Game
{
    private GraphicsDeviceManager _graphics;
    private SpriteBatch _spriteBatch;

    /// <summary>
    /// 动画文件
    /// </summary>
    public AnimatedSprite sprite;
    /// <summary>
    /// 绘制对象
    /// </summary>
    public Drawable drawObject;
    /// <summary>
    /// 摄像机
    /// </summary>
    public Camera camera;

    private const int SCREEN_WIDTH = 800;
    private const int SCREEN_HEIGHT = 600;

    public Game1()
    {
        _graphics = new GraphicsDeviceManager(this)
        {
            PreferredBackBufferWidth = SCREEN_WIDTH,
            PreferredBackBufferHeight = SCREEN_HEIGHT
        };
        _graphics.ApplyChanges();
        Content.RootDirectory = "Content";
        IsMouseVisible = true;
    }

    protected override void Initialize()
    {
        base.Initialize();
    }

    protected override void LoadContent()
    {
        _spriteBatch = new SpriteBatch(GraphicsDevice);
        var playerTexture = Content.Load<Texture2D>("playerMove");
        sprite = new AnimatedSprite(playerTexture, 1, 5, 0.1f, 0);
        // 创建精灵动画
        drawObject = new Drawable(sprite, new Vector2(0, 0), new Vector2(0, 0), 1f, 0, SpriteEffects.None, 10, null, 1);
        // 创建自定义绘制对象
        camera = new Camera(_spriteBatch, new Rectangle(0, 0, 800, 600), new Vector2(0, 0), new Vector2(800, 600));
        // 创建摄像机
    }

    protected override void Update(GameTime gameTime)
    {
        base.Update(gameTime);
        camera.insertObject(drawObject);
        // 调用添加绘制图像委托
        drawObject.currentAnimation.Update(gameTime);
        // 更新动画
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        _spriteBatch.Begin();
        camera.Draw();
        _spriteBatch.End();
        base.Draw(gameTime);
    }
}
